<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*小型健身房管理系统*/
class Admin extends AdminBase{
	private $admin;	
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		echo $this->fetch();
	}	
	function add(){
		$shop_id=sp_get_current_shop_id();
		$list=db("AuthGroup")->field("id,name")->where("shop_id=$shop_id")->fetchsql(FALSE)->select();
		$this->assign("menus",$list);
		echo $this->fetch();
	}
	/*新增员工*/
	function add_post(){
		if(request()->isPost()){
			$this->varify_uniq();
			$shop_id=sp_get_current_shop_id();
			$_POST['shop_id']=$shop_id;
			$admin_id=model("Admin")->InsertGetId(
					[
						"name"=>input("name"),
						"account"=>input("account"),
						"mobile"=>input("mobile"),
						"password"=>md5(input("password")),
						"is_boss"=>input("is_boss"),
						"shop_id"=>$shop_id,
					]
			);
			//新增权限
			db('Auth')->insert(array('admin_id'=>$admin_id,'shop_id'=>$shop_id,'auth_group_id'=>input('auth_group_id')));
			$this->ajax_success("成功!");	
		}else{
			$this->ajax_error("非法操作!");
		}
	}
	/*修改员工信息*/
	function edit(){
		$shop_id=sp_get_current_shop_id();
		$list=db("AuthGroup")->field("id,name")->where("shop_id=$shop_id")->select();
		$this->assign("menus",$list);
		
		$id=input("id");
		$this->assign("auth_group_id",db("Auth")->where("admin_id=".input("id"))->value("auth_group_id"));
		$this->assign("model",db("admin")->where("id=$id")->find());
		
		echo $this->fetch();			
	}
	function edit_post(){
		db("Auth")->where("admin_id=".input("id"))->update(array("auth_group_id"=>input("auth_group_id")));
		unset($_POST['auth_group_id']);
		$data=db('Admin')->fetchSql(FALSE)->update($_POST);
		$this->ajax_success("成功！");	
	}
	/*个人信息*/
	function info(){
		$admin_id=sp_get_current_admin()['id'];
		if(!empty(input('id'))){//修改个人信息
			$data=db('Admin')->fetchSql(FALSE)->update($_POST);
			$this->ajax_success(0);
		}
		$this->assign("model",db('admin')->where("id=$admin_id")->find());
		echo $this->fetch();
	}
	/*密码修改信息*/
	function edit_psw(){
		if(request()->isPost()){
			$admin_id=sp_get_current_admin_id();
			$old_password=db("admin")->where("id=$admin_id")->value("password");
			if($old_password!=md5(input("old_password"))){
				$this->ajax_error("原密码错误");
			}
			db("admin")->where("id=$admin_id")->setField(array("password"=>md5(input("password"))));
			$this->ajax_success(0);
		}else{
			echo $this->fetch();	
		}
	}
	
	function delete(){
		if(request()->isPost()){
			$id=input("id");
			$admin=db("admin")->where("id=$id")->find();
			if($admin['is_shop_admin']==1){
				$this->ajax_error("不能删除店铺管理员账号");
				die;
			}
			db("admin")->where("id=$id")->delete();
			$this->ajax_success("成功");
		}else{
			$this->ajax_error("非法操作！");
		}
	}
	
	/*员工列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	/**验证账号唯一**/
	function varify_uniq(){
		$account=input("account",'');
		$mobile=input("mobile",'');
		$count=db("admin")->where("account='$account'")->count(1);
		if($count>0){
			$this->ajax_error("账号已经存在");
		}
		$count=db("admin")->where("mobile='$mobile'")->count(1);
		if($count>0){
			$this->ajax_error("手机号已经存在");
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10061000");
		$this->assign("menus",$auth_group);
	}
	
	/*获取员工数据*/
	private function _ajax_index(){
		$query=array('name'=>array("field" =>"name","operator" =>"="),
					 'mobile'=>array("field" =>"mobile","operator" =>"="),
					 'account'=>array("field" =>"account","operator" =>"="),
					 );
		$shop_id=sp_get_current_shop_id();			 
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and account!='admin' ";
		if(!empty($shop_id)){
			$where_and.=" and shop_id=$shop_id";
		}else{
			$where_and.=" and shop_id=0";
		}
		$total=db("admin")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("admin")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		//权限
		$authgroups=db("Admin")->alias("a")->join([["m_auth b","a.id=b.admin_id and a.shop_id=$shop_id","left"],["m_auth_group c","b.auth_group_id=c.id and b.shop_id=$shop_id","left"]])->fetchsql(FALSE)->column("a.id,c.name");
		foreach ($list as $key => $value) {
			if(isset($authgroups[$value['id']])){
				$list[$key]['auth_name']=$authgroups[$value['id']];
			}else{
				if(empty($shop_id)){
					$list[$key]['auth_name']="管理员无法看到";
				}else if($value['is_shop_admin']==1){
					$list[$key]['auth_name']="店铺管理员";	
				}else{
					$list[$key]['auth_name']="无权限";
				}
				
			}
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
